package com.coocaa.x.service.liteconfig;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.RemoteException;

import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.liteconfig.aidl.ILiteConfigService;

/**
 * Created by lu on 15-7-29.
 */
public class LiteConfigServiceStub extends ILiteConfigService.Stub {
    private SharedPreferences sp = null;
    private Context mContext = null;

    public LiteConfigServiceStub(Context c) {
        mContext = c;
        sp = c.getSharedPreferences("lite_config", Context.MODE_WORLD_READABLE
                + Context.MODE_WORLD_WRITEABLE);
    }

    @Override
    public void whoAmI() {
        LogUtils.d("wai", "<<LiteConfigService>> who am i??  " + mContext.getPackageName());
    }

    @Override
    public boolean getBooleanValue(String key, boolean v) throws RemoteException {
        boolean value = v;
        synchronized (sp) {
            try {
                value = sp.getBoolean(key, v);
            } catch (Exception e) {
                value = v;
                e.printStackTrace();
            }
        }
        return value;
    }

    @Override
    public int getIntValue(String key, int v) throws RemoteException {
        int value = v;
        synchronized (sp) {
            try {
                value = sp.getInt(key, v);
            } catch (Exception e) {
                value = v;
                e.printStackTrace();
            }
        }
        return value;
    }

    @Override
    public long getLongValue(String key, long v) throws RemoteException {
        long value = v;
        synchronized (sp) {
            try {
                value = sp.getLong(key, v);
            } catch (Exception e) {
                value = v;
                e.printStackTrace();
            }
        }
        return value;
    }

    @Override
    public String getStringValue(String key, String v) throws RemoteException {
        String value = v;
        synchronized (sp) {
            try {
                value = sp.getString(key, v);
            } catch (Exception e) {
                value = v;
                e.printStackTrace();
            }
        }
        return value;
    }

    @Override
    public void saveBooleanConfig(String key, boolean value) throws RemoteException {
        synchronized (sp) {
            SharedPreferences.Editor editor = sp.edit();
            editor.putBoolean(key, value);
            editor.commit();
        }
    }

    @Override
    public void saveIntConfig(String key, int value) throws RemoteException {
        synchronized (sp) {
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt(key, value);
            editor.commit();
        }
    }

    @Override
    public void saveLongConfig(String key, long value) throws RemoteException {
        synchronized (sp) {
            SharedPreferences.Editor editor = sp.edit();
            editor.putLong(key, value);
            editor.commit();
        }
    }

    @Override
    public void saveStringConfig(String key, String value) throws RemoteException {
        synchronized (sp) {
            SharedPreferences.Editor editor = sp.edit();
            editor.putString(key, value);
            editor.commit();
        }
    }

}
